package nl.boelen.game.screen;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.Map;
import java.util.TreeMap;
import javax.swing.JPanel;
import nl.boelen.game.Game;
import nl.boelen.game.handler.MouseHandler;
import nl.boelen.game.screen.menubutton.LoadButton;
import nl.boelen.game.screen.menubutton.MenuButton;
import nl.boelen.game.screen.menubutton.NewButton;
import nl.boelen.game.screen.menubutton.OptionsButton;
import nl.boelen.game.screen.menubutton.QuitButton;
import nl.boelen.game.screen.menubutton.ResumeButton;
import nl.boelen.game.screen.menubutton.StopButton;

public class MenuScreen extends JPanel {
    private Game game;
    private MouseHandler mouseHandler;
    private Map<Integer, MenuButton> buttons = new TreeMap<Integer, MenuButton>();

    private GradientPaint painter;

    public MenuScreen(Game gameFrame, MouseHandler mouseHandler) {
        this.game = gameFrame;
        this.mouseHandler = mouseHandler;

        setSize(gameFrame.getWidth(), gameFrame.getHeight());
        setLayout(null);

        init();
    }

    public void init() {
        buttons.put(1, new NewButton(game, "New Game"));
        buttons.put(2, new LoadButton(game, "Load"));
        buttons.put(3, new ResumeButton(game, "Resume"));
        buttons.put(4, new SaveButton(game, "Save"));
        buttons.put(5, new OptionsButton(game, "Options"));
        buttons.put(6, new StopButton(game, "Stop"));
        buttons.put(7, new QuitButton(game, "Quit"));

        for (MenuButton button : buttons.values()) {
            add(button);
        }

        setBackground(Color.BLACK);
        painter = new GradientPaint(0, 0, Color.DARK_GRAY, 0, getHeight(), Color.LIGHT_GRAY);
    }

    @Override
    public void setVisible(boolean aFlag) {
        render();
        super.setVisible(aFlag);
    }

    public void render() {
        int i = 0;
        int center = getWidth() / 2;
        for (MenuButton button : buttons.values()) {
            button.setVisibility(game.getGameState());
            if (button.isVisible()) {
                button.setButtonLocation(center, i * 40 + 20);
                i++;
            }
        }
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (painter != null) {
            Dimension d = getSize();
            Graphics2D g2 = (Graphics2D) g;
            g2.setPaint(painter);
            g2.fill(new Rectangle(0, 0, d.width, d.height));
        }
    }
}
